import axios from 'axios'
import Vue from 'vue'
import router from './router'
const http = axios.create({
  //后台接口
  // baseURL: 'http://localhost:3000/admin'
  baseURL: 'http://localhost:3000/admin'
})
http.interceptors.request.use(function (config) {
  // Do something before request is sent
  //授权信息
  if (localStorage.token){
    config.headers.Authorization = 'Bearer ' + localStorage.token
  }
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

//给请求加一个拦截器
http.interceptors.response.use(res => {
  return res
}, err => {
  // console.log(err.response.data.message)
  if(err.response.data.message){
    Vue.prototype.$message({
      type: 'error',
      message: err.response.data.message
    })
    // console.log(err.response)
    if(err.response.status === 401){
      router.push('/login')
    }
  }
  return Promise.reject(err)

})
export default http
